package kz.kstu.ds.settings;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Date: 04.11.12
 * Time: 8:26
 *
 * @author Oleg Anastassov
 */
public final class SettingsManager {

    public static final String APP_NAME = "app.name";
    public static final String APP_VERSION = "app.version";
    public static final String APP_BUILD = "app.build";

    private static SettingsManager instance;
    private static Properties props;

    private SettingsManager() {
    }

    public static SettingsManager getInstance() {
        if (instance == null) {
            loadSettings();
            instance = new SettingsManager();
        }
        return instance;
    }

    private static void loadSettings() {
        InputStream is = SettingsManager.class.getResourceAsStream("/app.properties");
        props = new Properties();
        try {
            props.load(is);
        } catch (IOException e) {
            props = loadDefaults();
        }
    }

    public String getValue(String key) {
        return props.getProperty(key);
    }

    public static Properties loadDefaults() {
        Properties res = new Properties();
        res.put(APP_NAME, "<default name>");
        res.put(APP_VERSION, "<default version>");
        return res;
    }

}
